<?php

require_once 'business/AccountManager.php';

class ProfileController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
    }

    public function listAction() {
        $email = Business_AccountManager::getUserIdentity();
        $accountID = Application_Model_AccountMapper::getIDByEmail($email);
        $orders = Application_Model_OrderMapper::fetchByType($accountID);
        $this->view->orders = $orders;
    }

    public function infoAction() {
        $request = $this->getRequest();
        if ($request->isPost()) {
            try {
                $name = $request->getParam(P_NAME);
                $email = $request->getParam(P_EMAIL);
                $oldPassword = $request->getParam(P_OLD_PASSWORD);
                $password = $request->getParam(P_PASSWORD);
                $phone = $request->getParam(P_PHONE);
                $address = $request->getParam(P_ADDRESS);
                $account = new Application_Model_Account($email, md5($password), "U");
                if (Application_Model_AccountMapper::update($account, $oldPassword)) {
                    $profile = new Application_Model_Profile($name, $address, $phone, 0);
                    Application_Model_ProfileMapper::update($profile);
                }
            } catch (Exception $e) {
                $this->view->error = true;
            }
        }
        $email = Business_AccountManager::getUserIdentity();
        $profile = Application_Model_ProfileMapper::getByEmail($email);
        $this->view->profile = $profile;
        $this->view->email = $email;
    }

}

